package com.cn.por.config.security;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import com.fasterxml.jackson.databind.ObjectMapper;
/**
 * 自动定义登陆失败处理器
 * @author wen
 *
 */
public class PorAuthenticationFailureHandler implements AuthenticationFailureHandler {

	@Override
	public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
			AuthenticationException exception) throws IOException, ServletException {
		response.setContentType("application/json;charset=utf-8");
		Map<String,String> result = new HashMap<String,String>();
		result.put("code", "500");
		result.put("message", "登陆失败："+exception.getMessage());
		result.put("status", "500");
		result.put("msg", "登陆失败");
		ObjectMapper om = new ObjectMapper();
		String resStr = om.writeValueAsString(result);
		response.getWriter().write(resStr);
	}

}
